﻿#region

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class CustomerRegistrationForm : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var subsidiaries = SubsidiaryBLL.GetAllSubsidiariesWithName();
                ddlBranch.DataTextField = "BranchName";
                ddlBranch.DataValueField = "SubsidiaryId";
                ddlBranch.DataSource = subsidiaries;
                ddlBranch.DataBind();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtFName.Text = "";
            txtLName.Text = "";
            txtUname.Text = "";
            txtPassword.Text = "";
            txtRePassword.Text = "";
            txtEmail.Text = "";
            txtTel.Text = "";
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    var user = new User
                    {
                        Username = txtUname.Text,
                        Password = txtPassword.Text
                    };

                    if (ddlCustomerType.SelectedValue.Equals("Personal"))
                    {
                        var customer = new PersonalCustomer
                        {
                            FirstName = txtFName.Text,
                            LastName = txtLName.Text,
                            EMail = txtEmail.Text,
                            TelephoneNo = txtTel.Text,
                            Type = ddlCustomerType.Text,
                            Username = txtUname.Text,
                            SubsidiaryId = Convert.ToByte(ddlBranch.SelectedValue)
                        };
                        PersonalCustomerBLL.RegisterPersonalCustomer(customer, user);
                        Response.Redirect("Customers.aspx", false);
                    }
                    else
                    {
                        var customer = new BusinessCustomer
                        {
                            CompanyName = txtCompanyName.Text,
                            EMail = txtEmail.Text,
                            TelephoneNo = txtTel.Text,
                            Type = ddlCustomerType.Text,
                            Username = txtUname.Text,
                            SubsidiaryId = Convert.ToByte(ddlBranch.SelectedValue)
                        };
                        BusinessCustomerBLL.RegisterBusinessCustomer(customer, user);
                        Response.Redirect("Customers.aspx", false);
                    }
                }
                catch (Exception)
                {
                    Response.Write("<script>Alert('Something went wrong');</script>");
                }
            }
        }

        protected void CustomValidatorUsername_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = !UserBLL.IsUsernameTaken(txtUname.Text);
        }

        protected void CustomValidatorEmail_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = !CustomerBLL.IsEmailTaken(txtEmail.Text);
        }

        protected void ddlCustomerType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCustomerType.SelectedValue.Equals("Personal"))
            {
                panelPersonalCustomer.Visible = true;
                panelBusinessCustomer.Visible = false;
            }
            else
            {
                panelBusinessCustomer.Visible = true;
                panelPersonalCustomer.Visible = false;
            }
        }
    }
}